/*=================================================
    File:	IMManager.h

	Module:	IMManager
	Author:	Mike	

=================================================*/

#ifndef	_IM_MANAGER_H_
#define _IM_MANAGER_H_

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include <config.h>

#define MAX_IM_SIZE		1024	//1k is enough	

typedef struct _IM_INBOX
{
	bool	bRead;
	char	date[32];
	char	time[32];
	char	msg[MAX_IM_SIZE];
	char	number[CFG_PHONEBOOK_DIGIT_MAX];

	//reserved
//	char	domain[CFG_NETWORK_SERVER_MAX];
//	unsigned  port;

} IM_INBOX;

typedef struct _IM_OUTBOX
{
	char	date[32];
	char	time[32];
	char	msg[MAX_IM_SIZE];
	char	number[CFG_PHONEBOOK_DIGIT_MAX];

	//reserved
//	char	domain[CFG_NETWORK_SERVER_MAX];
//	unsigned  port;
} IM_OUTBOX;

typedef struct _IM_DRAFTBOX
{
	char	msg[MAX_IM_SIZE];
} IM_DRAFTBOX;

//error code for IM (<0)
#define	IM_ERR_NOT_INIT			-1
#define	IM_ERR_NO_ROOM			-2
#define	IM_ERR_ACCESS_FLASH		-3
#define	IM_ERR_PARAM_INVALID	-4
#define IM_ERR_NOT_AVAIL		-5
#define IM_ERR_OP_FAIL			-6

//for InBox
bool  IM_InBox_GetSize(int * used, int * total);
int   IM_InBox_AddMsg(char * number, char * msg); //return index, -1 for error
//int   IM_InBox_AddMsg(char * number, char * domain, unsigned port, char * msg); //reserved
bool  IM_InBox_GetMsgByIndex(int index, IM_INBOX * pMsg);
bool  IM_InBox_DeleteMsgByIndex(int index);
bool  IM_InBox_Cleanup();
//For special op.
bool  IM_InBox_GetMsgNumberInfoByIndex(int index, char * number, int size);
bool  IM_InBox_GetMsgReadFlagByIndex(int index, bool * isRead);
bool  IM_InBox_SetMsgReadFlagByIndex(int index);

//for OutBox
bool  IM_OutBox_GetSize(int * used, int * total);
bool  IM_OutBox_GetMsgByIndex(int index, IM_OUTBOX * pMsg);
bool  IM_OutBox_DeleteMsgByIndex(int index);
bool  IM_OutBox_Cleanup();
//For special op.
bool  IM_OutBox_GetMsgNumberInfoByIndex(int index, char * number, int size);


//for DraftBox
bool  IM_DraftBox_GetSize(int * used, int * total);
int   IM_DraftBox_AddMsg(char * msg); //return index, <0 for error
bool  IM_DraftBox_GetMsgByIndex(int index, IM_DRAFTBOX * pMsg);
bool  IM_DraftBox_DeleteMsgByIndex(int index);
bool  IM_DraftBox_Cleanup();

//other op
int IMManager_Initialize();

//send msg (and save msg to outbox)  
int   IMManager_SendOutMsg(char * number, char * msg); //return index, <0 for error
//int   IMManager_SendOutMsg(char * number, char * domain, unsigned port, char * msg); //reserved
bool IMManager_CanSend();

//for draft edit issue...
//BUGBUG: add for poor flash update issue
// That is, if you add one just after delete a message, it may fail to save to flash!
//by Mike
int   IM_DraftBox_UpdateMsg(int index, char * msg);

//
//Add if Inbox/Outbox also needs to do update.
//
// int   IM_InBox_UpdateMsg(int index, char * msg);
// int   IM_OutBox_UpdateMsg(int index, char * msg);

//for new message issue
bool  IM_InBox_GetNewMsgFlag();
void  IM_InBox_ClearNewMsgFlag();

#endif //_IM_MANAGER_H_
